#include "ResManager.h"

#ifdef _DEBUG
#define RESFILE "..\\..\\res\\game1.zip"
#else
#define RESFILE ".\\game1.zip"
#endif

#define SAFEDELETE(p) {if(p){delete p; p = NULL;}}

CResManager* CResManager::m_resManager = NULL;

CResManager::CResManager(void)
:m_hge(NULL),m_res(NULL)
{
	m_hge = hgeCreate(HGE_VERSION);
	if(m_hge && m_hge->Resource_AttachPack(RESFILE))
	{
		m_res = new hgeResourceManager("game1.res");
		if(m_res == NULL)
		{
			m_resManager = NULL;
			MessageBox(NULL,"Init Res error...", "ResError", MB_OK);
		}
		else
		{
			LoadRes();
		}
	}
	else
	{
		m_resManager = NULL;
	}
}

CResManager::~CResManager(void)
{
	SAFEDELETE(m_res);
	m_hge->Resource_RemoveAllPacks();
	m_hge->Release();
	m_hge = NULL;
}

CResManager* CResManager::GetInstance()
{
	if(m_resManager == NULL)
	{
		m_resManager = new CResManager();
	}
	return m_resManager;
}

void CResManager::LoadRes()
{
	//player1
	HTEXTURE tex;
	tex = m_res->GetTexture("player1_tex");
	for(int i = 0; i < TANK_W_COUNT; i++)
		for(int j = 0; j < TANK_H_COUNT; j++)
			m_spritePlay1[i][j] = new hgeSprite(tex, i*TANK_W, j*TANK_H, TANK_W, TANK_H);

	//bullet
	tex = m_res->GetTexture("bullet_tex");
	for(int i = 0; i < BULLET_W_COUNT; i++)
		for(int j = 0; j < BULLET_H_COUNT; j++)
			m_spriteBullet[i][j] = new hgeSprite(tex, i*BULLET_W, j*BULLET_H, BULLET_W, BULLET_H);

	//enemy
	tex = m_res->GetTexture("enemy_tex");
	for(int i = 0; i < TANK_W_COUNT; i++)
		for(int j = 0; j < TANK_H_COUNT*2; j++)
			m_spriteEnemy[i][j] = new hgeSprite(tex, i*TANK_W, j*TANK_H, TANK_W, TANK_H);

	//map
	tex = m_res->GetTexture("map_tex");
	for(int i = 0; i < MAP_UNIT_COUNT; i++)
		m_spriteMapUnit[i] = new hgeSprite(tex, i*UNIT_W, 0, UNIT_W, UNIT_H);
	
	//explode
	m_animExplode = m_res->GetAnimation("explode");
}